package aoptest.schema1;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;

public class ProfilingAdvice {
	
	public Object log(ProceedingJoinPoint pjp) throws Throwable {
		// start the stop watch
		StopWatch sw = new StopWatch();
		sw.start(pjp.getSignature().getName());
		Object returnValue = pjp.proceed();
		sw.stop();
		
		dumpInfo(pjp, sw.getTotalTimeMillis());
		return returnValue;
	}

	private void dumpInfo(ProceedingJoinPoint pjp, long ms) {
		Object[] args = pjp.getArgs();
		System.out.println("Executed method: " + pjp.getSignature().getName());
		System.out.println("On object of type: " + pjp.getTarget().getClass().getName());
		System.out.println("With arguments:");
		for (int x = 0; x < args.length; x++) {
			System.out.print(" > " + args[x]);
		}
		System.out.print("\n");
		System.out.println("Took: " + ms + " ms");
	}

}
